import { usePermission } from "@/compositions/main/usePermission";
import { useUrlBreaker } from "@/compositions/main/useUrlBreaker";
import { ref } from "vue";
import { addBtnPermission } from "@/utils";
export interface IToRouteOptions {
  specificPage?: string, // 指定地址
}

export function useMenu() {
  const store = useStore()
  const router = useRouter()
  const route = useRoute()

  const { condition } = usePermission()
  const { serviceType } = useUrlBreaker()

  // const topMenuList = ref<Array<any>>([])
  const showSide = ref(false);

  const defaultActive = computed<string>(() => {
    return route.path;
  });

  const toRoute = (id: string, options?: IToRouteOptions) => {
    if ([ "AD", "MATERIAL" ].includes(id)) {
      ElMessage.info("应用正在开发中，敬请期待...");
      return
    }

    if (!condition(id)) {
      ElMessage.info("待开通，请联系商务人员...");
      return
    }

    console.log('route', route)
    if (id == store.state.user.currentService) {
      ElMessage.warning("您正在当前平台中...");
      return
    } 
    
    const temp = store.state.user.allPermission.find((i: any) => i.serviceType == id);

    addBtnPermission(temp.permissionLayer, temp.serviceType + "Btn");

    if (!temp.permissionLayer.length) {
      router.push("/403");
      return
    }

    store.commit('SETCURRENTSERVICE', temp.serviceType)
    document.getElementById('func-tab').style.left = '0px';
    // topMenuList.value = temp.permissionLayer
    if (options?.specificPage) {
      router.push(options.specificPage)
      store.commit('SETBOOKLIST', {
        path: options?.specificPage,
        service: store.state.user.currentService
      })
    } else if (temp.permissionLayer[0].type == "NODE") {
      router.push(temp.permissionLayer[0].children[0].frontRouter)
      store.commit('SETBOOKLIST', {
        path: temp.permissionLayer[0].children[0].frontRouter,
        service: store.state.user.currentService
      })
    } else if (temp.permissionLayer[0].type == "PAGE") {
      router.push(temp.permissionLayer[0].frontRouter)
      store.commit('SETBOOKLIST', {
        path: temp.permissionLayer[0].frontRouter,
        service: store.state.user.currentService,
      })
    }
  }

  const showSideMenu = () => {
    showSide.value = !showSide.value;
  };

  return {
    defaultActive,
    showSide,

    toRoute,
    showSideMenu
  }
}
